package com.ldj.struts.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.MenuBuilder;
import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.common.Upload;
import com.ldj.hibernate.EmployerContractInfo;
import com.ldj.hibernate.EmployerContractInfoDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.FunctionMenuDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UserAreaScope;
import com.ldj.hibernate.ViewCaseDAO;
import com.ldj.struts.form.EmployerContractInfoForm;

public class EmployerContractInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		List list = dao.findAllPage(request.getParameter("eid"), eForm, null);
		request.setAttribute("list", list);
		EmployerInfoDAO edao = new EmployerInfoDAO(request, response);
		request.setAttribute("eInfo", edao.findById(request.getParameter("eid")));
		return mapping.findForward("list");
	}
	public ActionForward inteStatTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		MenuBuilder bu = new MenuBuilder();
		String value = "";
		FunctionMenuDAO dao = new FunctionMenuDAO(request,response);
		List<UserAreaScope> infoList = dao.findByUserId(SystemSession.getSupUserId(request));
		if(infoList != null && infoList.size()>0){
			UserAreaScope info1 =infoList.get(0);
			value = info1.getUserAreaScopeAreaId();
		}
		request.setAttribute("tree", bu.getPowerAreaTree(request, response, "行政区划", "", "inteStatEmployerContractInfo.do?method=inteStatList&code=$code",value));		
		return mapping.findForward("inteStatTree");
	}
	public ActionForward inteStatList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		List list = dao.findAllPage(null, eForm, request.getParameter("code"));
		request.setAttribute("list", list);
		return mapping.findForward("list");
	}	
	
	public ActionForward myList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		FunctionMenuDAO fdao = new FunctionMenuDAO(request,response);
		String scorp = "";
		List ulist = fdao.findByUserId(SystemSession.getSupUserId(request));
		if(ulist != null && ulist.size() > 0){
			UserAreaScope uas = (UserAreaScope)ulist.get(0);
			scorp = uas.getUserAreaScopeAreaId();
			if(scorp!=null && !"".equals(scorp)){
				scorp = scorp.replace(",", "','");
			}
			else{
				scorp="";
			}
		}
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		List list = dao.findMyPage(scorp, eForm);
		request.setAttribute("list", list);
		return mapping.findForward("myList");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo ecinfo = dao.findById(request.getParameter("ecid"));
		EmployerInfoDAO dao2 = new EmployerInfoDAO(request, response);
		EmployerInfo einfo = dao2.findById(ecinfo.getEmployerInfo().getEmpInfoId());
		request.setAttribute("eInfo", einfo);
		request.setAttribute("ecInfo", ecinfo);
		return mapping.findForward("detail");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("toAdd");
	}
	
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
		request.setAttribute("eInfo", info);
		return mapping.findForward("toUpdate");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
		info.setEmpConEmpName(eForm.getEmpConEmpName());
		info.setEmpConEmpSex(eForm.getEmpConEmpSex());
		info.setEmpConIdCardNum(eForm.getEmpConIdCardNum());
		info.setEmpConEducationLevel(eForm.getEmpConEducationLevel());
		info.setEmpConHouse(eForm.getEmpConHouse());
		info.setEmpConAddress(eForm.getEmpConAddress());
		info.setEmpConPhone(eForm.getEmpConPhone());
		info.setEmpConWordType(eForm.getEmpConWordType());
		info.setEmpConWorkType(eForm.getEmpConWorkType());
		info.setEmpConCompetence(eForm.getEmpConCompetence());
		info.setEmpConEmpDate(eForm.getDateEmpConEmpDate());
		info.setEmpConContractTime(eForm.getEmpConContractTime());
		info.setEmpConContractBeginTime(eForm.getDateEmpConContractBeginTime());
		info.setEmpConContractEndTime(eForm.getDateEmpConContractEndTime());
		info.setEmpConPost(eForm.getEmpConPost());
		info.setEmpConContractFileUrl(eForm.getEmpConContractFileUrl());
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("1");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerContractInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",info.getEmpConEmpName()));
		logDao.save(s);
		return new ActionForward("/EmployerContractInfo.do?method=list&eid=" + info.getEmployerInfo().getEmpInfoId(), true);
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoForm eForm = (EmployerContractInfoForm)form;
		EmployerContractInfo info = new EmployerContractInfo();
		info.setEmpConEmpName(eForm.getEmpConEmpName());
		info.setEmpConEmpSex(eForm.getEmpConEmpSex());
		info.setEmpConIdCardNum(eForm.getEmpConIdCardNum());
		info.setEmpConEducationLevel(eForm.getEmpConEducationLevel());
		info.setEmpConHouse(eForm.getEmpConHouse());
		info.setEmpConAddress(eForm.getEmpConAddress());
		info.setEmpConPhone(eForm.getEmpConPhone());
		info.setEmpConWordType(eForm.getEmpConWordType());
		info.setEmpConWorkType(eForm.getEmpConWorkType());
		info.setEmpConCompetence(eForm.getEmpConCompetence());
		info.setEmpConEmpDate(eForm.getDateEmpConEmpDate());
		info.setEmpConContractTime(eForm.getEmpConContractTime());
		info.setEmpConContractBeginTime(eForm.getDateEmpConContractBeginTime());
		info.setEmpConContractEndTime(eForm.getDateEmpConContractEndTime());
		info.setEmpConPost(eForm.getEmpConPost());
		info.setEmpConContractFileUrl(eForm.getEmpConContractFileUrl());
		info.setEmpConContractType("0");
		info.setEmpConDelOper("0");
		info.setEmpConState("0");
		EmployerInfoDAO edao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = edao.findById(request.getParameter("eid"));
		info.setEmployerInfo(eInfo);
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		dao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerContractInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",info.getEmpConEmpName()));
		logDao.save(s);
		return new ActionForward("/EmployerContractInfo.do?method=list&eid=" + info.getEmployerInfo().getEmpInfoId(), true);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		EmployerContractInfo info = dao.findById(request.getParameter("ecid"));
		info.setEmpConDelOper("1");
		dao.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "EmployerContractInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",info.getEmpConEmpName()));
		logDao.save(s);
		return new ActionForward("/EmployerContractInfo.do?method=list&eid=" + info.getEmployerInfo().getEmpInfoId(), true);
	}
	
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			HashMap map = Upload.upload(request, "contract", ".jpg;.gif");
			if(map.containsKey("fileName")){
				request.setAttribute("fileName", (String)map.get("fileName"));
			}else{
				request.setAttribute("error", "error");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapping.findForward("upload");
	}
	
	
	public ActionForward unitContractStatisticsFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		int endYear = y + 2;
		request.setAttribute("endYear", endYear);
		request.setAttribute("nowYear", y);
		return mapping.findForward("unitContractStatisticsFind");
	}
	//(企业)签订劳动合同情况统计表
	public ActionForward unitContractStatisticsDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		List list = dao.findFromCode(areaCode);
		request.setAttribute("list", list);
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("unitContractStatisticsDetail");
	}
	
	public ActionForward unitContractStatisticsXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		EmployerContractInfoDAO dao = new EmployerContractInfoDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		List list = dao.findFromCode(areaCode);
		request.setAttribute("list", list);
		request.setAttribute("organ", SystemSession.getUserOrgan(request));
		return mapping.findForward("unitContractStatisticsXls");
	}
	
	
	
}